﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SurfaceCardsBoardLibrary.Interfaces;
using System.Windows;
using Microsoft.Surface.Presentation.Controls;
using System.Windows.Media.Animation;
using SurfaceCardsGameKit.Tools;
using SurfaceCardsBoardLibrary;

namespace SurfaceCardsGameKit.Dispositions
{
    class StackDisposition : IDisposition
    {

        public StackDisposition()
        {
            CardsNumberOffset = 5;
        }

        public int CardsNumberOffset { get; set; }

        #region IDisposition Members

        public void Render(IDeck deck, Size cardSize, Rect destinationRect, Point center,
            double deckOrientation, bool useTransitions, bool sizeChanged, DeckOrientation deckupDownOrientation, double durationRatio)
        {
            Point actualCenter;
            Point deckMiddle = new Point(destinationRect.X + destinationRect.Width / 2, destinationRect.Y + destinationRect.Height / 2);

            var i = 0;
            foreach (var card in deck)
            {
                if (card.ScatterViewItem.Visibility == Visibility.Collapsed)
                    continue;

                var svi = card.ScatterViewItem;
                Point centerScatterView = new Point(deckMiddle.X, deckMiddle.Y);

                centerScatterView.X += CardsNumberOffset / -2.0 + i;

                // calc the center
                if (i < CardsNumberOffset)
                    i++;

                if (card.IsSelected)
                    centerScatterView.Y += deckupDownOrientation == DeckOrientation.Up ? -20 : 20;

                // calc real center
                actualCenter = deck.ScatterViewItem.TranslatePoint(centerScatterView, deck.ScatterView);

                bool centerChanged = svi.Center != actualCenter;

                if (useTransitions)
                {
                    double orientation;

                    // calc orientation
                    if (sizeChanged)
                        orientation = DispositionHelper.CalcOrientationForCards(svi.ActualOrientation, deckOrientation);
                    else
                        orientation = svi.ActualOrientation;

                    bool orientationChanged = svi.ActualOrientation != deckOrientation;

                    var duration = DispositionHelper.ComputeAnimationDuration(
                        svi.ActualCenter, actualCenter, svi.ActualOrientation, deckOrientation, durationRatio);

                    // center animation
                    if (centerChanged)
                        DispositionHelper.BeginCenterAnimation(svi, actualCenter, duration);

                    // orientation animation
                    if (orientationChanged)
                        DispositionHelper.BeginOrientationAnimation(svi, orientation, duration);
                }
                else
                {
                    double orientation = deckOrientation;
                    bool orientationChanged = svi.Orientation != deckOrientation;

                    if (centerChanged)
                    {
                        DispositionHelper.StopCenterAnimation(svi);
                        card.Center = actualCenter;
                    }
                    if (orientationChanged)
                    {
                        DispositionHelper.StopOrientationAnimation(svi);
                        card.Orientation = orientation;
                    }
                }

            }

        }

        public bool CanApplyDisposition(Size cardSize, Rect destinationRect, int cardsCount)
        {
            return true;
        }

        #endregion
    }
}
